import snap


def iteration():
    # create a directed random graph on 100 nodes and 1k edges
    G2 = snap.GenRndGnm(snap.TNGraph, 100, 1000)
    # traverse the nodes
    for NI in G2.Nodes():
        print("node id %d with out-degree %d and in-degree %d" % (
            NI.GetId(), NI.GetOutDeg(), NI.GetInDeg()))
    # traverse the edges
    for EI in G2.Edges():
        print("edge (%d, %d)" % (EI.GetSrcNId(), EI.GetDstNId()))
    # traverse the edges by nodes
    for NI in G2.Nodes():
        for Id in NI.GetOutEdges():
            print("edge (%d %d)" % (NI.GetId(), Id))


def initGra():
    # 下四种图不允许存在平行边
    # TNGraph is a directed graph   TNGraphNodeI    TNGraphEdgeI
    # TUNGraph is a undirected graph   TUNGraphNodeI    TUNGraphEdgeI
    # TUndirNet is a undirected graph   TUndirNetNodeI  TUndirNetEdgeI
    # TDirNet  a directed graph     TDirNetNodeI    TDirNetEdgeI
    # 下种图可以存在平行边
    # TNEANet is a directed multigraph  TNEANetNodeI    TNEANetEdgeI
    G1 = snap.TNGraph.New()
    G1.AddNode(1)
    G1.AddNode(5)
    G1.AddNode(32)
    G1.AddEdge(1, 5)
    G1.AddEdge(5, 1)
    G1.AddEdge(5, 32)
    # for NI in G1.Nodes():
    #     print(NI.GetId())
    #     print(NI.GetOutDeg())
    #     print(NI.GetInDeg())
    #     print()
    print(G1.GetNI(1).GetOutNId(0))  # 返回第n个出点
    print(G1.GetNI(1).GetInNId(0))  # 返回第n个入点
    print(G1.GetNI(32).GetInNId(0))
    print(G1.GetNI(32).IsOutNId(5))  # 是否存在32->5这个边
    print(G1.GetNI(32).IsInNId(5))
    print(G1.GetNI(32).IsNbrNId(5))  # 相邻无关方向
    print(G1.GetNI(5).IsNbrNId(32))

    print(G1.GetEI(1, 5).GetSrcNId())
    print(G1.GetEI(1, 5).GetDstNId())

    labels = {}
    for NI in G1.Nodes():
        labels[NI.GetId()] = str(NI.GetId())
    G1.DrawGViz(snap.gvlDot, "output.png", " ", labels)


def FileGra():
    # generate a network using Forest Fire model
    G3 = snap.GenForestFire(1000, 0.35, 0.35)
    # save and load binary
    FOut = snap.TFOut("test.graph")
    G3.Save(FOut)
    FOut.Flush()
    FIn = snap.TFIn("test.graph")
    G4 = snap.TNGraph.Load(FIn)
    # save and load from a text file
    snap.SaveEdgeList(G4, "test.txt", "Save as tab-separated list of edges")
    G5 = snap.LoadEdgeList(snap.TNGraph, "test.txt", 0, 1)


def forestFire():
    # generate a network using Forest Fire model
    G6 = snap.GenForestFire(10, 0.35, 0)

    labels = {}
    for NI in G6.Nodes():
        labels[NI.GetId()] = str(NI.GetId())
    G6.DrawGViz(snap.gvlDot, "forestFire.png", " ", labels)

    # convert to undirected graph
    G7 = G6.ConvertGraph(snap.TUNGraph)
    WccG = G6.GetMxWcc()
    # get a subgraph induced on nodes [0,1,2,3,4,5]
    SubG = G6.GetSubGraph([0, 1, 2, 3, 4])
    # get 3-core of G
    Core3 = G6.GetKCore(3)
    # delete nodes of out degree 10 and in degree 5
    G6.DelDegKNodes(10, 5)


if __name__ == "__main__":
    forestFire()
